import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

class LoadingButton extends HookWidget {
  Future<dynamic> Function() onPressed;
  final Widget child;

  LoadingButton({
    Key? key,
    required this.child,
    required this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var loadingState = useState(false);
    var buttonStyle = const ButtonStyle();
    buttonStyle.backgroundColor;
    return ElevatedButton(
      style: buttonStyle,
      onPressed: () async {
        if(!loadingState.value){
          loadingState.value = true;
          try {
            await onPressed();
          } finally {
            loadingState.value = false;
          }
        }
      },
      child: loadingState.value
          ? Stack(
              alignment: Alignment.center,
              children: [
                child,
                if (loadingState.value)
                  Container(
                    padding: EdgeInsets.all(4),
                    child: CircularProgressIndicator(
                      strokeWidth: 3,
                    ),
                  ),
              ],
            )
          : child,
    );
  }
}
